package com.quanyan.user.web;

import com.alibaba.fastjson.JSONObject;
import com.quanyan.api.APIResponse;
import com.quanyan.comment.enums.TaskEnum;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.mq.client.QcMsgClient;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.user.model.vo.response.RespUserCenter;
import com.quanyan.user.model.vo.response.RespUserTask;
import com.quanyan.user.mq.UserTaskMsg;
import com.quanyan.user.service.UserTaskCenterService;
import com.quanyan.user.service.UserTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by jian.zhou on 2016/7/2.
 * 任务
 */
@RestController
@RequestMapping("/api/user/task")
public class UserTaskController {

    private static final Logger logger = LoggerFactory.getLogger(UserTaskController.class);

    @Autowired
    private UserTaskCenterService userTaskCenterService;

    @Autowired
    private UserTaskService userTaskService;

    @Resource(name = "qcMsgClient")
    private QcMsgClient qcMsgClient;

    //获取任务和宝箱
    @RequestMapping(value = "list1" , method = RequestMethod.POST)
    public APIResponse<RespUserCenter> queryRespUserCenter(@RequestBody Map map,HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        try {
            RespUserCenter respUserCenter = userTaskCenterService.queryTaskAndBox(uid);
            return APIResponse.returnSuccess(respUserCenter);
        } catch (Exception e) {
            logger.error("",e);
            return APIResponse.returnFail("系统异常！");
        }

    }


    //获取任务和宝箱
    @RequestMapping(value = "list" , method = RequestMethod.POST)
    public APIResponse<RespUserCenter> queryRespUserCenter(HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        try {
            RespUserCenter respUserCenter = userTaskCenterService.queryTaskAndBox(uid);
            return APIResponse.returnSuccess(respUserCenter);
        } catch (Exception e) {
            logger.error("",e);
            return APIResponse.returnFail("系统异常！");
        }

    }


    //获取新手任务
    @RequestMapping(value = "newList" , method = RequestMethod.POST)
    public APIResponse<JSONObject> queryNewTask(HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        try {
            List<RespUserTask> respUserCenter = userTaskService.queryUserNewHandTask(uid);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("respNewUserTaskList",respUserCenter);
            return APIResponse.returnSuccess(jsonObject);
        } catch (Exception e) {
            logger.error("",e);
            return APIResponse.returnFail("系统异常！");
        }

    }




    //做任务 分享
    @RequestMapping(value = "task" , method = RequestMethod.POST)
    public APIResponse whileTask(@RequestBody Map map,HttpServletRequest request){
        int uid =  Integer.parseInt(map.get("uid").toString());
        Integer taskId = Integer.parseInt(map.get("taskId").toString());
        try {
             userTaskService.whileTask(uid,taskId);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("",e);
            return APIResponse.returnFail("系统异常！");
        }

    }

    //做任务 分享
    @RequestMapping(value = "doTask" , method = RequestMethod.POST)
    public APIResponse doTask(HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        try {
            userTaskService.whileTask(uid,TaskEnum.TASK_ENUM__SHARE.getId());
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("",e);
            return APIResponse.returnFail("系统异常！");
        }

    }


    //获取任务和宝箱
    @RequestMapping(value = "doTaskC" , method = RequestMethod.POST)
    public APIResponse whileTask(@RequestBody @Valid UserTaskMsg userTaskMsg ,BindingResult bindingResult ){
        if(bindingResult.hasErrors())
            return APIResponse.returnFail("参数不能为空！");
        logger.info("c++开始发送消息到mq:{}",userTaskMsg.toString());
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        mqRequestMsg.setTopic(Constants.MQ_TOPIC_TASK_TOPIC);
        mqRequestMsg.setTag(Constants.MQ_TOPIC_TASK_TAG);
        mqRequestMsg.setBody(ObjectUtil.toByteArray(userTaskMsg));
        qcMsgClient.asyncSend(mqRequestMsg);
        logger.info("c++接收消息完毕==========》");
        return APIResponse.returnSuccess();
    }



    @RequestMapping(value = "kafka" ,method = RequestMethod.POST)
    public APIResponse test(@RequestBody UserTaskMsg userTaskMsg){
        //messageSendOne.sendUserInfo(JSONObject.toJSONString(userTaskMsg),"play");
        return APIResponse.returnSuccess();
    }




    @RequestMapping(value = "query" ,method = RequestMethod.POST)
    public APIResponse query(HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        try {
            return APIResponse.returnSuccess(userTaskCenterService.getTaskCenter(uid));
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常!");
        }
    }





}
